import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import Grid from "../../redare/components/grid/Grid";
import {Button, Col, Row, Space} from "antd";
import OnlFormHeadPresenter from "../../.gen/presenter/OnlFormHeadPresenter";
import moment from "moment";
import {Link} from "react-router-dom";


export default class OnlFormHeadGrid extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new OnlFormHeadPresenter(this)
    }

    componentDidMount() {
        this._findOnlFormHeadPage()
    }

    //搜索
    _findOnlFormHeadPage(searchParams, page) {
        if (this.grid) {
            this.grid.showLoadingIndicator()
        }
        this.searchParams = searchParams;
        if (!this.searchParams) {
            this.searchParams = {}
        }
        if (page) {
            this.curPage = page;
        }
        this.presenter.findOnlFormHeadPage(
            this.searchParams,
            {curPage: this.curPage, pageSize: this.pageSize},
            (result) => {
                let {data} = result;
                if (data && this.grid) {
                    let {result, curPage, pageSize, totalCount, totalPage} = data;
                    this.totalCount = totalCount;
                    this.totalPage = totalPage;
                    this.grid.setData(result, {curPage, pageSize, totalCount})
                }
            },
            () => {
                if (this.grid) {
                    this.grid.closeLoadingIndicator()
                }
            }
        )
    }

    _editOnlFormHead(id) {
        if (this.props.onItemEdit) {
            this.props.onItemEdit(id);
        }
    }

    //删除
    _delOnlFormHead(id) {
        this.confirm("确定删除吗？", () => {
            this.presenter.deleteOnlFormHead({id: id}, {}, () => {
                this.toastSuccess("删除成功");
                this._findOnlFormHeadPage(this.searchParams, 1);
            })
        })
    }

    //批量删除
    _batchDel = () => {
        let {ids} = this;
        if (!ids || ids.length <= 0) {
            this.toastWarning("请选择数据");
            return
        }
        this.confirm("确定删除吗？", () => {
            this.presenter.batchDeleteOnlFormHead({ids: ids}, {},
                () => {
                    this.toastSuccess("批量删除成功");
                    this._findOnlFormHeadPage(this.searchParams, 1);
                })
        })
    };


    //分页
    _changePage(curPage, pageSize) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this._findOnlFormHeadPage(this.searchParams)
    }

    onSelectChange = selectedRowKeys => {
        this.setState({selectedRowKeys});
    };

    getData(){
        if (this.grid){
            return this.grid.getData();
        }
    }

    getSelectRows(){
        const {selectedRowKeys} = this.state;
        return selectedRowKeys;
    }

    render() {
        const {selectedRowKeys} = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };
        return (
            <>
                <Grid ref={(o) => {
                    this.grid = o
                }}
                      border={true}
                      _initLoading={false}
                      search={false}
                      scroll={{x: 'max-content'}}
                      rowKey={(v) => v.id}
                      _pageSize={50}
                      _onPageChange={(page) => {
                          this._changePage(page.curPage, page.pageSize)
                      }}
                      rowSelection={rowSelection}
                      columns={[
                          {
                              dataIndex: 'tableName',
                              width: 150,
                              title: '表名'
                          },
                          {
                              dataIndex: 'tableType',
                              width: 150,
                              title: '表类型',
                              render: (value, record, index) => {
                                  if (value === 0) {
                                      return '单表';
                                  } else if (value === 1) {
                                      return '主表';
                                  } else if (value === 2) {
                                      return '附表';
                                  }
                              }
                          },
                          {
                              dataIndex: 'comment',
                              width: 150,
                              title: '表说明'
                          },
                          {
                              dataIndex: 'tableVersion',
                              width: 150,
                              title: '表版本'
                          },
                          {
                              title: '创建时间',
                              dataIndex: 'createTime',
                              width: 150,
                              render: (value, record, index) => {
                                  return value ? moment(value).format("YYYY-MM-DD HH:mm:ss") : '';
                              }
                          },
                          {
                              title: '操作',
                              width: 180,
                              fixed: 'right',
                              render: (value, record, index) => {
                                  return (
                                      <Space size="middle">
                                          {/*<Link to={{*/}
                                          {/*    pathname: "/onlformhead/info",*/}
                                          {/*    search: "?id=" + record.id*/}
                                          {/*}}>查看</Link>*/}
                                          <a onClick={this._editOnlFormHead.bind(this, record.id)}>编辑</a>
                                          <a onClick={this._delOnlFormHead.bind(this, record.id)}>删除</a>
                                      </Space>
                                  )
                              }
                          }
                      ]}
                />
            </>

        )
    }

}




